from django.db.models import Q
from django.forms import model_to_dict

# 查询得到的queryset(list格式)转换为list
from thickcharts.models import Device


def querylist_to_list(items):
    items_list = []
    for item in items:
        items_list.append(item)
    return items_list

# 查询的query（queryset）集转换为list(json)
def query_to_list(query):
    querylist = []
    for item in query:
        querylist.append(model_to_dict(item))
    return querylist

# 根据关键词query搜索devices
def devices_filter(query):
    query = query.strip()
    con = Q()
    if query:
        q1 = Q(gateway__contains=query)
        q2 = Q(devicename__contains=query)
        q3 = Q(probe__contains=query)
        q4 = Q(tube_name__contains=query)
        con.add(q1, 'OR')
        con.add(q2, 'OR')
        con.add(q3, 'OR')
        con.add(q4, 'OR')
    devices_query = Device.objects.filter(con)
    return devices_query



